// gjw/pages/index/register.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    message:'验证码',
    currentTime:60,
    yan:'yan',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
			siteUrl: wx.getStorageSync('siteUrl')
		})
		that.logo()
  },

  logo:function(e){
    var that=this
    app.util.request({
      url:'entry/wxapp/logo',
      success:function(res){
        
        that.setData({
          logo: res.data.res
        })
      }
    })
  },
  change:function(e){
    this.setData({
      phone: e.detail.value
    })
    
  },
  //倒计时
  getCode: function (e) {
    var that = this;
    var yes = true
    var no=false
    //输入的手机号
    var currentNum = that.data.phone;
    var currentTime = that.data.currentTime;
    var interval = setInterval(function () {
      currentTime--;
      that.setData({
        message: '剩' + currentTime + '秒',
        disabled: yes,
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          message: "重新获取",
          currentTime: 60,
          disabled: no,
          yan:'yan',
        })
      }
    }, 1000)
  },
  get_code:function(e){
    var that = this;
    var compphone = that.data.phone;
    var myreg = /^0{0,1}(13[0-9]|15[0-9]|18[0-9]|17[0-9])[0-9]{8}$/;

    if (!myreg.test(compphone)) {
      wx.showToast({
        title: '您输入的手机号有误!',
        icon: "none",
      })
    } else {
      that.setData({
        yan: 'on_yan'
      })
      that.getCode();
      // // 获取6位数的随机数
      var recive_code = "";
      for (var i = 0; i < 6; i++) {
        recive_code += Math.floor(Math.random() * 10);
      }
      that.setData({
        disabled: true,
        recive_code: recive_code
      })
      // 随机数传给后台
      app.util.request({
        'url': 'entry/wxapp/Code',
        'cachetime': '0',
        data: {
          code: recive_code,
          phone: compphone
        },
        success: function (res) {
          

        }
      })
    }
  },

  zhuce:function(e){
    var that=this
    var openid = wx.getStorageSync('openid')
    var avatarUrl = wx.getStorageSync('avatarUrl')
    var data=e.detail.value
    var rag = /^[\u4E00-\u9FA5]{1,10}$/
    var reg = /^0{0,1}(13[0-9]|15[0-9]|18[0-9]|17[0-9])[0-9]{8}$/
    var new_code = that.data.recive_code
    var username = data.username
    var phone = data.phone
    var code=data.code
    if (username=='') {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none',
      })
    }else if (!(rag.test(username))) {
        wx.showToast({
          title: '用户名须是真实姓名（不少于2个字符）',
          icon: 'none',
        })
    }else if(phone==''){
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
      })
    } else if (!(reg.test(phone))) {
        wx.showToast({
          title: '手机号格式不正确',
          icon: 'none',
        })
    }else if(code==''){
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
      })
    } else if (code != new_code) {
      wx.showToast({
        title: '验证码有误',
        icon: 'none',
      })
    }else{
      app.util.request({
        'url':'entry/wxapp/register',
        'data':{
          openid: openid, avatar: avatarUrl,phone:phone,username:username
        },
        success:function(res){
          
          if(res.data.code==200){
            wx.showToast({
              title: '注册成功',
            })
						wx.navigateBack({
							delta: 1
						})
          } else if (res.data.code == 201){
            wx.showToast({
              title: '该用户已经注册',
              icon: 'none',
            })
          }
        }

      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})